package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class PZUtil {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static Connection con;
	static{
		Properties pop=new Properties();
		PZpath pzpath=new PZpath();
		InputStream pi=pzpath.getIns();
		try {
			pop.load(pi);
			driver=pop.getProperty("driver");
			url=pop.getProperty("url");
			user=pop.getProperty("user");
			password=pop.getProperty("password");
			System.out.println(driver);
			Class.forName(driver);
			con=DriverManager.getConnection(url, user, password);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		try {
			if(con==null||con.isClosed()){
				con=DriverManager.getConnection(url, user, password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return con;
	}
	public static void closeConnection(){
		try {
			if(con!=null||!con.isClosed())
				con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
